var administrator = {};

administrator.init = function() {
    administrator.active();
};

administrator.active = function() {
    $("td[for=active]").click(function() {
        var id = $(this).attr("ref");
        var _td = $(this);
        ajax({
            service: 'cpservice/administrator/changeactive.json',
            data: {id: id},
            loading: false,
            done: function(resp) {
                if (resp.success) {
                    var html = resp.data.active ? '<label class="label label-success">Hoạt động</label>' : '<label class="label label-danger">Tạm khóa</label>';
                    html += '<i style="cursor: pointer" class="glyphicon glyphicon-' + (resp.data.active ? 'check' : 'unchecked') + ' pull-right"></i>';
                    _td.html(html);
                } else {
                    popup.msg(resp.message);
                }
            }
        });
    });
};

administrator.grantRole = function(_id) {
    ajax({
        service: 'cpservice/administrator/getrolebyadministratorId.json',
        data: {id: _id},
        loading: false,
        done: function(resp) {
            if (resp.success) {
                popup.open("popup-cpfunction", "Cấp quyền", template('/cp/tpl/administrator/grantrole.tpl', resp), [
                    {
                        title: 'Cấp quyền',
                        style: 'btn-info',
                        fn: function() {
                            var form = new Object();
                            form.id = _id;
                            form.roleId = [];
                            $.each($("input[for=check_all]"), function() {
                                if ($(this).is(":checked")) {
                                    form.roleId.push($(this).attr("groupId"));
                                }
                            });
                            $.each($("input[for=function]"), function() {
                                if ($(this).is(":checked")) {
                                    form.roleId.push($(this).attr("functionId"));
                                }
                            });
                            ajax({
                                service: 'cpservice/administrator/grantrole.json',
                                data: form,
                                type: 'post',
                                contentType: 'json',
                                done: function(resp) {
                                    if (resp.success) {
                                        popup.msg(resp.message, function() {
                                            location.reload();
                                        });
                                    } else {
                                        popup.msg(resp.message);
                                    }
                                }
                            });
                            popup.close('popup-cpfunction');
                        }
                    },
                    {
                        title: 'Đóng',
                        style: 'btn-default',
                        fn: function() {
                            popup.close('popup-cpfunction');
                        }
                    }
                ]);

                setTimeout(function() {
                    $("input[for=check_all]").click(function() {
                        var id = $(this).attr("groupId");
                        if ($(this).is(":checked")) {
                            $("input[group=" + id + "]").attr({"checked": true});
                        } else {
                            $("input[group=" + id + "]").attr({"checked": false});
                        }
                    });

                    $.each(resp.data, function() {
                        if ($("input[groupId=" + this.cpfunctionid + "]").length > 0) {
                            $("input[groupId=" + this.cpfunctionid + "]").attr({"checked": "true"});
                        }
                        if ($("input[functionId=" + this.cpfunctionid + "]").length > 0) {
                            $("input[functionId=" + this.cpfunctionid + "]").attr({"checked": "true"});
                        }
                    });

                }, 100);

            } else {
                popup.msg(resp.message);
            }
        }
    });
};